<?php
/**
 * @title 1418. 点菜展示表 - 优化
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param String[][] $orders
     * @return String[][]
     */
    function displayTable($orders) {
        /**
         * @since 2021-07-06 菜品和餐桌
         */
        $foodArray = $tableArray = [];
        $foodHashs = $tableHashs = [];

        /**
         * @since 2021-07-06 遍历
         */
        foreach ($orders as list($customerName, $tableNumber, $foodItem)){
            $foodArray[$foodItem] = 1;
            $tableArray[$tableNumber] = 1;
        }
        // return $tableArray;

        /**
         * @since 2021-07-06 菜品排序
         */
        $foodItemArray = array_keys($foodArray);
        sort($foodItemArray);

        /**
         * @since 2021-07-06 返回数据
         */
        $result = [];
        // $result[] = array_merge(["Table"], $foodItemArray);
        $tmpArray = ["Table"];
        for($i=0; $i<count($foodItemArray); $i++){
            $foodItem = $foodItemArray[$i];
            $tmpArray[] = $foodItem;

            /**
             * @since 2021-07-06 记录菜品序号
             */
            $foodHashs[$foodItem] = $i+1;
        }
        $result[] = $tmpArray;

        /**
         * @since 2021-07-06 餐桌排序
         */
        $tableNumberArray = array_keys($tableArray);
        sort($tableNumberArray);

        /**
         * @since 2021-07-06 初始化数组
         */
        $initArray = array_fill(0, count($result[0]), "0");

        /**
         * @since 2021-07-06 添加餐桌
         */
        for($i=0; $i<count($tableNumberArray); $i++){
            $tableNumber = $tableNumberArray[$i];
            $initArray[0] = strval($tableNumber);
            $result[] = $initArray;

            /**
             * @since 2021-07-06 记录每桌的序号
             */
            $tableHashs[$tableNumber] = $i+1;
        }

        /**
         * @since 2021-07-06 遍历, 菜品数量+1
         */
        foreach ($orders as list($customerName, $tableNumber, $foodItem)){
            $x = $tableHashs[$tableNumber];
            $y = $foodHashs[$foodItem];

            $result[$x][$y] = strval($result[$x][$y]+1);
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202107/2021.07.06_2.php
 */
$datas = [
    [[["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->displayTable(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
